<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <!--      <el-form-item label="商家id" prop="shopName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.shopName"-->
      <!--          clearable-->
      <!--          placeholder="请输入商家id"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="货源类型" prop="commodityOriginType">-->
      <!--        <el-select v-model="queryParams.commodityOriginType" clearable placeholder="请选择货源类型">-->
      <!--          <el-option-->
      <!--            v-for="dict in dict.type.commodity_origin_type"-->
      <!--            :key="dict.value"-->
      <!--            :label="dict.label"-->
      <!--            :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="货源(关联采购表)" prop="commodityId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.commodityId"-->
      <!--          clearable-->
      <!--          placeholder="请输入货源(关联采购表)"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="产品采购数量" prop="commodityNum">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.commodityNum"-->
      <!--          clearable-->
      <!--          placeholder="请输入产品采购数量"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="产品唯一标识" prop="commodityUuid">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.commodityUuid"-->
      <!--          clearable-->
      <!--          placeholder="请输入产品唯一标识"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购员ID" prop="staffId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.staffId"-->
      <!--          clearable-->
      <!--          placeholder="请输入采购员ID"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购员" prop="commodityStaff">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.commodityStaff"-->
      <!--          clearable-->
      <!--          placeholder="请输入采购员"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="采购时间" prop="shipTime">-->
      <!--        <el-date-picker v-model="queryParams.shipTime"-->
      <!--                        clearable-->
      <!--                        placeholder="请选择采购时间"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--        >-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核员ID" prop="checkId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.checkId"-->
      <!--          clearable-->
      <!--          placeholder="请输入审核员ID"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核员" prop="checkStaff">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.checkStaff"-->
      <!--          clearable-->
      <!--          placeholder="请输入审核员"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="审核时间" prop="checkTime">-->
      <!--        <el-date-picker v-model="queryParams.checkTime"-->
      <!--                        clearable-->
      <!--                        placeholder="请选择审核时间"-->
      <!--                        type="date"-->
      <!--                        value-format="yyyy-MM-dd"-->
      <!--        >-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="防伪码" prop="secturityCode">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.secturityCode"-->
      <!--          clearable-->
      <!--          placeholder="请输入防伪码"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="商品名称" prop="commodityName">
        <el-input
          v-model="queryParams.commodityName"
          clearable
          placeholder="请输入商品名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名称" prop="merchantName">
        <el-input
          v-model="queryParams.merchantName"
          clearable
          placeholder="请输入商户名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户地址" prop="merchantLocation">
        <el-input
          v-model="queryParams.merchantLocation"
          clearable
          placeholder="请输入商户地址"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品种类" prop="commodityType">
        <el-select v-model="queryParams.commodityType" clearable placeholder="请选择产品种类">
          <el-option
            v-for="dict in dict.type.commodity_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="产品审核状态" prop="commodityStatus">
        <el-select v-model="queryParams.commodityStatus" clearable placeholder="请选择产品审核状态">
          <el-option
            v-for="dict in dict.type.commodity_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产日期" prop="produceDate">
        <el-date-picker clearable v-model="queryParams.produceDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择生产日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="过期时间" prop="produceOverdate">
        <el-date-picker clearable v-model="queryParams.produceOverdate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择过期时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['trace:productretail:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['trace:productretail:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['trace:productretail:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['trace:productretail:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productretailList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="商品名称" prop="commodityName"/>
      <el-table-column align="center" label="商户名称" prop="merchantName"/>
      <el-table-column align="center" label="商户地址" prop="merchantLocation"/>
      <el-table-column align="center" label="产品种类" prop="commodityType">-->
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_type" :value="scope.row.commodityType"/>
        </template>
      </el-table-column>

      <!--      <el-table-column label="产品d" align="center" prop="id"/>-->
      <!--      <el-table-column label="商家id" align="center" prop="shopName"/>-->
      <!--      <el-table-column label="货源类型" align="center" prop="commodityOriginType">-->
      <!--        <template slot-scope="scope">-->
      <!--          <dict-tag :options="dict.type.commodity_origin_type" :value="scope.row.commodityOriginType"/>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="货源(关联采购表)" align="center" prop="commodityId"/>-->
      <!--      <el-table-column label="产品采购数量" align="center" prop="commodityNum"/>-->
      <!--      <el-table-column label="产品唯一标识" align="center" prop="commodityUuid"/>-->
      <!--      <el-table-column label="采购员ID" align="center" prop="staffId"/>-->
      <!--      <el-table-column label="采购员" align="center" prop="commodityStaff"/>-->
      <!--      <el-table-column label="采购时间" align="center" prop="shipTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.shipTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="审核员ID" align="center" prop="checkId"/>-->
      <!--      <el-table-column label="审核员" align="center" prop="checkStaff"/>-->
      <!--      <el-table-column label="审核时间" align="center" prop="checkTime" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="备注" align="center" prop="remark"/>-->
      <!--      <el-table-column label="防伪码" align="center" prop="secturityCode"/>-->
      <el-table-column align="center" label="产品审核状态" prop="commodityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.commodity_status" :value="scope.row.commodityStatus"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="生产时间" prop="produceDate"/>
      <el-table-column align="center" label="过期时间" prop="produceOverdate"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['trace:productretail:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['trace:productretail:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页查看-->
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改商品零售详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家id" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入商家id" @focus="nameFocus"/>
        </el-form-item>
        <el-form-item label="货源类型" prop="commodityOriginType">
          <el-select v-model="form.commodityOriginType" placeholder="请选择货源类型">
            <el-option
              v-for="dict in dict.type.commodity_origin_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="货源(关联采购表)" prop="commodityId">
          <el-input v-model="form.commodityId" placeholder="请输入货源(关联采购表)" @focus="nameIntoFocus"/>
        </el-form-item>
        <el-form-item label="产品采购数量" prop="commodityNum">
          <el-input v-model="form.commodityNum" placeholder="请输入产品采购数量"/>
        </el-form-item>
        <el-form-item label="产品唯一标识" prop="commodityUuid">
          <el-input v-model="form.commodityUuid" placeholder="请输入产品唯一标识"/>
        </el-form-item>
        <el-form-item label="采购员ID" prop="staffId">
          <el-input v-model="form.staffId" placeholder="请输入采购员ID"/>
        </el-form-item>
        <el-form-item label="采购员" prop="commodityStaff">
          <el-input v-model="form.commodityStaff" placeholder="请输入采购员"/>
        </el-form-item>
        <el-form-item label="采购时间" prop="shipTime">
          <el-date-picker v-model="form.shipTime"
                          clearable
                          placeholder="请选择采购时间"
                          type="date"
                          value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="审核员ID" prop="checkId">
          <el-input v-model="form.checkId" placeholder="请输入审核员ID"/>
        </el-form-item>
        <el-form-item label="审核员" prop="checkStaff">
          <el-input v-model="form.checkStaff" placeholder="请输入审核员"/>
        </el-form-item>
        <el-form-item label="审核时间" prop="checkTime">
          <el-date-picker v-model="form.checkTime"
                          clearable
                          placeholder="请选择审核时间"
                          type="date"
                          value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="防伪码" prop="secturityCode">
          <el-input v-model="form.secturityCode" placeholder="请输入防伪码"/>
        </el-form-item>
        <el-form-item label="产品审核状态" prop="commodityStatus">
          <el-select v-model="form.commodityStatus" placeholder="请选择产品审核状态">
            <el-option
              v-for="dict in dict.type.commodity_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!--    零售商户子组件-->
    <el-dialog :before-close="inputDialogClose" :visible.sync="inputProdDialog" title="零售商户信息" width="70%">
      <inputPRod @uploadProdData="receiveProdData"></inputPRod>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inputProdDialog = false">取 消</el-button>
        <el-button type="primary" @click="inputDecideOn">确 定</el-button>
      </span>
    </el-dialog>
    <!--    商品采购详情弹出框-->
    <el-dialog :before-close="inputDialogClose" :visible.sync="inputIntoDialog" title="商品采购详情" width="70%">
      <inputInto @uploadIntoData="receiveIntoData"></inputInto>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inputIntoDialog = false">取 消</el-button>
        <el-button type="primary" @click="inputIntoDecideOn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductretail,
  getProductretail,
  delProductretail,
  addProductretail,
  updateProductretail
} from '@/api/trace/productretail'

import inputPRod from './inputPRod'
import inputInto from './inputInto'

export default {
  name: 'Productretail',
  dicts: ['commodity_status', 'commodity_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品零售详情表格数据
      productretailList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shopName: null,
        commodityOriginType: null,
        commodityId: null,
        commodityNum: null,
        commodityUuid: null,
        staffId: null,
        commodityStaff: null,
        shipTime: null,
        checkId: null,
        checkStaff: null,
        checkTime: null,
        secturityCode: null,
        commodityStatus: null,
        commodityName: null,
        companyNameOut: null,
        companyNameInto: null,
        commodityType: null,
        merchantName: null,
        merchantLocation: null,
        produceDate: null,
        produceOverdate: null
      },
      // 表单参数
      form: {
        shopName: '',
        commodityId: ''
      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: '产品id不能为空', trigger: 'blur' }
        ],
        shopName: [
          { required: true, message: '商家id不能为空', trigger: 'blur' }
        ],
        commodityOriginType: [
          { required: true, message: '货源类型不能为空', trigger: 'change' }
        ],
        commodityId: [
          { required: true, message: '货源(关联采购表)不能为空', trigger: 'blur' }
        ],
        commodityNum: [
          { required: true, message: '产品采购数量不能为空', trigger: 'blur' }
        ],
        commodityUuid: [
          { required: true, message: '产品唯一标识不能为空', trigger: 'blur' }
        ],
        staffId: [
          { required: true, message: '采购员ID不能为空', trigger: 'blur' }
        ],
        commodityStaff: [
          { required: true, message: '采购员不能为空', trigger: 'blur' }
        ],
        shipTime: [
          { required: true, message: '采购时间不能为空', trigger: 'blur' }
        ],
        checkId: [
          { required: true, message: '审核员ID不能为空', trigger: 'blur' }
        ],
        checkStaff: [
          { required: true, message: '审核员不能为空', trigger: 'blur' }
        ],
        checkTime: [
          { required: true, message: '审核时间不能为空', trigger: 'blur' }
        ],
        secturityCode: [
          { required: true, message: '防伪码不能为空', trigger: 'blur' }
        ],
        commodityStatus: [
          { required: true, message: '产品审核状态不能为空', trigger: 'change' }
        ]
      },
      // 控制零售商的弹出对话框
      inputProdDialog: false,
      // 商家id数据
      ProdForm: {},
      // 控制商品采购详情的弹出对话框
      inputIntoDialog: false,
      // 商品采购id数据
      ProdIntoFrom: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询商品零售详情列表 */
    getList() {
      this.loading = true
      listProductretail(this.queryParams).then(response => {

        this.productretailList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        shopName: null,
        commodityOriginType: null,
        commodityId: null,
        commodityNum: null,
        commodityUuid: null,
        staffId: null,
        commodityStaff: null,
        shipTime: null,
        checkId: null,
        checkStaff: null,
        checkTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        secturityCode: null,
        commodityStatus: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品零售详情'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getProductretail(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改商品零售详情'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProductretail(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addProductretail(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除商品零售详情编号为"' + ids + '"的数据项？').then(function() {
        return delProductretail(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('trace/productretail/export', {
        ...this.queryParams
      }, `productretail_${new Date().getTime()}.xlsx`)
    },
    // 点击对话框弹出的确认关闭对话框
    inputDialogClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {
        })
    },
    // 商家id获得焦点后触发
    nameFocus() {
      this.inputProdDialog = true
    },
    // 接收商家id
    receiveProdData(val) {
      // console.log(val)
      this.ProdForm = val
    },
    // 零售商户点击确定后触发
    inputDecideOn() {
      this.form.shopName = this.ProdForm.id
      this.inputProdDialog = false
    },
    // 商品采购点击确定后触发
    inputIntoDecideOn() {
      this.form.commodityId = this.ProdIntoFrom.id
      this.inputIntoDialog = false
    },
    // 商品采购输入框获得焦点后触发
    nameIntoFocus() {
      this.inputIntoDialog = true
    },
    // 接受商品采购表传来的值
    receiveIntoData(val) {
      this.ProdIntoFrom = val
    }
  },
  components: { inputPRod, inputInto }
}
</script>
